home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Science / RLaB / examples / collatz.r < prev    next >
Text File  |  1994-04-25  |  457b  |  35 lines

  1. //
  2. // The 3n + 1 problem
  3. //
  4.  
  5. collatz = function(start)
  6. {
  7.   local(c, n);
  8.   c = n = start;
  9.  
  10.   while(n > 1)
  11.   {
  12.     if(mod(n,2) == 0) {
  13.       n = n/2;
  14.     else
  15.       n = 3*n + 1;
  16.     }
  17.     c = [c, n];
  18.   }
  19.   return c;
  20. };
  21.  
  22. //
  23. // Try it out
  24. //
  25.  c = collatz(100);
  26.  
  27.  plgrid ();
  28.  ptitle ( "RLaB Collatz Example" );
  29.  xlabel ( "Independent Variable" );
  30.  ylabel ( "Dependent Variable" );
  31.  plot( [1:c.nc; c]' );
  32.  pause ();
  33.  plgrid ( ,"bcgnstlv");
  34.  plot( [1:c.nc; c]' );
  35.